package com.example.fundappcation.ui;


import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.fragment.app.DialogFragment;

import com.example.fundappcation.Base.BaseActivity;
import com.example.fundappcation.Receiver.NetWorkReceiver;
import com.example.fundappcation.bean.fundBean;
import com.example.fundappcation.databinding.ActivityMainBinding;
import com.example.fundappcation.net.ApiService;
import com.example.fundappcation.net.RetrofitUtils;
import com.example.fundappcation.weight.AddDialog;


import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class MainActivity extends BaseActivity implements AddDialog.onItemClickList {
    private ActivityMainBinding mBinding;
    private NetWorkReceiver netWorkReceiver;
    private long time;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected View initView() {
        mBinding = ActivityMainBinding.inflate(getLayoutInflater());
        return mBinding.getRoot();
    }

    @Override
    protected void init() {
        if (netWorkReceiver == null) {
            netWorkReceiver = new NetWorkReceiver();
        }
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(netWorkReceiver, intentFilter);
    }

    @Override
    protected void initLoad() {
        //获取时间戳
        time = System.currentTimeMillis();
        mBinding.tvClick.setOnClickListener(o -> {
            AddDialog addDialog = new AddDialog();
            addDialog.show(getSupportFragmentManager(), "jj");
            addDialog.setOnItemClickList(this);
        });
    }

    @Override
    protected void initData() {


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(netWorkReceiver);
    }

    @Override
    public void onConfirm(DialogFragment dialogFragment, String message) {
        dialogFragment.dismiss();
        query(message);


    }

    @Override
    public void onCancel(DialogFragment dialogFragment) {
        dialogFragment.dismiss();
    }

    private void query(String message) {
        RetrofitUtils.getInstance().create(ApiService.class)
                .queryFund(message, String.valueOf(time))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<fundBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull fundBean bean) {
                        Log.d("TAG 成功", bean.toString());

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.d("TAG", "失败了");

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}